package controller

import (
	"fmt"
	"net/http"
	"serialServer/utility"
	"serialServer/mysqlUtility"
	"encoding/json"
	"strconv"
)

// http://localhost/getLatestDataByDeviceId?device_id=aaa
// "DeviceId": "设备的Id，它需要可以配置"
//}
func GetLatestByDeviceId(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()  //解析参数，默认是不会解析的
	fmt.Println(r.Form)  //这些信息是输出到服务器端的打印信息

	fmt.Println(r.Form["device_id"])
	var deviceId string = "aaa"

	var resp utility.Resp
	var resultList []interface{}

	var thermocouple = mysqlUtility.GetLastestThermocoupleByDeviceId(deviceId)
	var theReturn utility.UniReturn
	theReturn.Value = ConvertFloat31ToString(thermocouple.Value)
	theReturn.Uuid = thermocouple.Uuid
	theReturn.DeviceId = thermocouple.DeviceId
	theReturn.InsertDatetime = thermocouple.InsertDatetime
	resultList = append(resultList, theReturn)

	var accelerometer = mysqlUtility.GetLastestAccelerometerByDeviceId(deviceId)
	var acceReturn utility.UniReturn
	acceReturn.Value = ConvertFloat31ToString(accelerometer.AcceRx) + "," + ConvertFloat31ToString(accelerometer.AcceRy)+ "," + ConvertFloat31ToString(accelerometer.AcceRz)
	acceReturn.Uuid = accelerometer.Uuid
	acceReturn.DeviceId = accelerometer.DeviceId
	acceReturn.InsertDatetime = accelerometer.InsertDatetime
	resultList = append(resultList, acceReturn)

	var gyroscope = mysqlUtility.GetLastestGyroscopeByDeviceId(deviceId)
	var geoReturn utility.UniReturn
	geoReturn.Value = ConvertFloat31ToString(gyroscope.GyroRx) + "," + ConvertFloat31ToString(gyroscope.GyroRy)+ "," + ConvertFloat31ToString(gyroscope.GyroRz)
	geoReturn.Uuid = gyroscope.Uuid
	geoReturn.DeviceId = gyroscope.DeviceId
	geoReturn.InsertDatetime = gyroscope.InsertDatetime
	resultList = append(resultList, geoReturn)

	var magnetic  = mysqlUtility.GetLastestMagneticByDeviceId(deviceId)
	var magReturn utility.UniReturn
	magReturn.Value = ConvertFloat31ToString(magnetic.MegnX) + "," + ConvertFloat31ToString(magnetic.MegnY)+ "," + ConvertFloat31ToString(magnetic.MegnZ)
	magReturn.Uuid = magnetic.Uuid
	magReturn.DeviceId = magnetic.DeviceId
	magReturn.InsertDatetime = magnetic.InsertDatetime
	resultList = append(resultList, magReturn)

	var barometer  = mysqlUtility.GetLastestBarometerByDeviceId(deviceId)
	var barReturn utility.UniReturn
	barReturn.Value = ConvertFloat31ToString(barometer.Value)
	barReturn.Uuid = barometer.Uuid
	barReturn.DeviceId = barometer.DeviceId
	barReturn.InsertDatetime = barometer.InsertDatetime
	resultList = append(resultList, barReturn)

	var electricResistance  = mysqlUtility.GetLastestElectricResistanceByDeviceId(deviceId)
	var eleReturn utility.UniReturn
	eleReturn.Value = ConvertFloat31ToString(electricResistance.Value)
	eleReturn.Uuid = electricResistance.Uuid
	eleReturn.DeviceId = electricResistance.DeviceId
	eleReturn.InsertDatetime = electricResistance.InsertDatetime
	resultList = append(resultList, eleReturn)

	resp.Result = resultList

	buf, _ := json.Marshal(resp)

	fmt.Fprintf(w, string(buf)) //这个写入到w的是输出到客户端的
}

// http://localhost/getRecentDataByDeviceIdAndStartDatetimeAndCount?device_id=aaa&start_datetime=2017-07-27 16:07:59&count=10
// http://localhost/getRecentDataByDeviceIdAndStartDatetimeAndCount?device_id=aaa&start_datetime=2017-06-27 16:07:59&count=10

//	"DeviceId": "设备的Id，它需要可以配置"

func GetRecentByDeviceIdAndStartDatetimeAndCount(w http.ResponseWriter, r *http.Request) {

	r.ParseForm()  //解析参数，默认是不会解析的
	fmt.Println(r.Form)  //这些信息是输出到服务器端的打印信息

	fmt.Println(r.Form["device_id"])
	var deviceId string = "aaa"
	var startDatetime string = r.Form["start_datetime"][0]
	var count, _ = strconv.Atoi(r.Form["count"][0])

	fmt.Println(deviceId)
	fmt.Println(startDatetime)
	fmt.Println(count)

	var resp utility.Resp
	var resultList []interface{}

	var thermocouple = mysqlUtility.GetRecentThermocoupleByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, thermocouple)

	var accelerometer = mysqlUtility.GetRecentAccelerometerByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, accelerometer)

	var gyroscope = mysqlUtility.GetRecentGyroscopeByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, gyroscope)

	var magnetic  = mysqlUtility.GetRecentMagneticByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, magnetic)

	var barometer  = mysqlUtility.GetRecentBarometerByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, barometer)

	var electricResistance  = mysqlUtility.GetRecentElectricResistanceByDeviceIdAndCount(deviceId, startDatetime, int32(count))
	resultList = append(resultList, electricResistance)

	resp.Result = resultList

	buf, _ := json.Marshal(resp)

	fmt.Fprintf(w, string(buf)) //这个写入到w的是输出到客户端的
}

func ConvertFloat31ToString(a float32)  string {
	return strconv.FormatFloat(float64(a), 'f', 1, 64)
}

